package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.service.AdminTopicService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminTopicServiceImpl implements AdminTopicService {
    @Override
    public List<MarketTopic> list(String page, String limit, String sort, String order, String title, String subtitle) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if(!StringUtils.isEmpty(title)){
            // select * from sxxx where username like '%xxx%'
            criteria.andTitleLike("%" + title + "%");
        }
        if(!StringUtils.isEmpty(subtitle)){
            // select * from sxxx where username like '%xxx%'
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        page="1";
        limit="2";
        marketTopicExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketTopic> marketTopics=topicMapper.selectByExample(marketTopicExample);
        session.commit();
        session.close();
        return marketTopics;
    }
}
